package edu.bbu.commonproject.desktop.gui.view.swing.file.lister.tree;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.UIDefaults;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

import edu.bbu.commonproject.desktop.gui.view.swing.utils.Design;
import edu.bbu.commonproject.desktop.gui.view.swing.utils.TexturedPanel;

/**
 * The tree directory.
 */
public class DirectoryTree extends JDialog
{
	/** It initializes the SELECTED_ANSWER*/
	public static final int SELECT_ANSWER = 0;
	
	/** It initializes the CANCEL_ANSWER*/
	public static final int CANCEL_ANSWER = 1;
	
	/** The tree. */
	private JTree tree;
	
	/** The scroll pane. */
	private JScrollPane scrollPane;
	
	/** The dafault tree node. */
	private DefaultMutableTreeNode rooTreeNode;
	
	/** the default tree cell. */
	private DefaultTreeCellRenderer treeCellRenderer;
	
	/** The text label. */
	private JLabel textLabel;
	
	/** The selected button. */
	private JButton selectButton;
	
	/** The cancel button. */
	private JButton canceButton;
	
	/** The back panel. */
	private TexturedPanel backPanel;
	
	/** It initializes the answer*/
	private int answer = CANCEL_ANSWER;
	
	/** It initializes the selectedDirectory */
	private String selectedDirectory = "";
	
	/** The separator. */
	private String separator;
	
	/**
	 * The constructor.
	 * @param parrentFrame the parrent frame.
	 * @param separator the separator.
	 */
	public DirectoryTree(JFrame parrentFrame, String separator)
	{
		super(parrentFrame, true);
		
		this.separator = separator;
		
		this.setResizable(false);
		this.setTitle("Synchronization directoryes");
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		
		setComponents();
		
		this.setLocationRelativeTo(parrentFrame);
	}

	/**
	 * It sets the components.
	 */
	public void setComponents()
	{
		rooTreeNode = new DefaultMutableTreeNode("Directoryes");
		
		tree = new JTree(rooTreeNode);
		tree.setRootVisible(false);
		tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
		
		Design.setTreeSelectedLook(tree);
		
		treeCellRenderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
		treeCellRenderer.setLeafIcon(new ImageIcon(Design.DIRECTORY_TREE_CLOSE));
		treeCellRenderer.setClosedIcon(new ImageIcon(Design.DIRECTORY_TREE_CLOSE));
		treeCellRenderer.setOpenIcon(new ImageIcon(Design.DIRECTORY_TREE_OPEN));
		
		scrollPane = new JScrollPane(tree);
		scrollPane.setLocation(20, 90);
		scrollPane.setSize(new Dimension(360, 250));
		
		
		canceButton = new JButton("Cancel");
		canceButton.setLocation(275, 363);
		canceButton.setSize(new Dimension(120, 30));
		canceButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				answer = CANCEL_ANSWER;
				close();
			}
		});
		
		selectButton = new JButton("Select");
		selectButton.setLocation(145, 363);
		selectButton.setSize(new Dimension(120, 30));
		selectButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				TreePath treePath = tree.getSelectionPath();
				
				if(treePath != null)
				{
					Object[] treePathObjects = treePath.getPath();
					selectedDirectory = "";
					
					for(int i = 1; i < treePathObjects.length; i++)
					{
						String directory = treePathObjects[i].toString();
						
						if(!directory.endsWith(separator))
						{
							selectedDirectory += directory + separator;
						}
						else 
						{
							selectedDirectory += directory;
						}	
					}
					
					answer = SELECT_ANSWER;
				}
				else 
				{
					answer = CANCEL_ANSWER;
				}
				
				close();
			}
		});
		
		textLabel = new JLabel("Select the synchronization folder!");
		textLabel.setLocation(125, 35);
		textLabel.setSize(new Dimension(200, 25));
		
		backPanel = new TexturedPanel(Design.DIRECTORY_DIALOG_BACK);
		backPanel.setPreferredSize(new Dimension(400, 400));
		backPanel.setLayout(null);
		
		backPanel.add(selectButton);
		backPanel.add(canceButton);
		backPanel.add(scrollPane);
		backPanel.add(textLabel);
		
		this.add(BorderLayout.CENTER, backPanel);
		this.pack();
	}
	
	/**
	 * It closes the frame.
	 */
	private void close()
	{
		this.setVisible(false);
		this.dispose();
	}
	
	/**
	 * It sets the click node listener.
	 * @param listener the click node listener.
	 */
	public void setNodeClickListener(MouseListener listener)
	{
		tree.addMouseListener(listener);
	}
	
	/**
	 * It adds to root.
	 * @param child the child name.
	 */
	public void addToRoot(DefaultMutableTreeNode child)
	{
		rooTreeNode.add(child);		
		((DefaultTreeModel) tree.getModel()).reload();
	}
	
	/**
	 * It gets the selected directory.
	 * @return the selected directory.
	 */
	public String getSelectedDirectory()
	{
		return selectedDirectory;
	}
	
	/**
	 * It sets the visibility to true.
	 */
	public void showDialog()
	{
		this.setVisible(true);
	}
	
	/**
	 * It gets the answer.
	 * @return the answer.
	 */
	public int getAnswer()
	{
		return answer;
	}
}
